import 'package:enterprise_v6_app/app/components/video/fijkplayer_skin.dart';
import 'package:enterprise_v6_app/app/components/video/schema.dart';
import 'package:enterprise_v6_app/app/components/video/video_config.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/common/job_ticket_type.dart';
import 'package:enterprise_v6_app/app/data/entities/application/execute_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/standing_book/operating_ledger_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/standing_book/receipt_info_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:fijkplayer/fijkplayer.dart';

class OperatingLedgerDetailsController extends GetxController {
  OperatingLedgerReceiptLists operatingLedgerReceiptLists = Get.arguments as OperatingLedgerReceiptLists;


  final FijkPlayer player = FijkPlayer();

  /// 点击的视频索引
  int videoIndex = 0;

  /// 视频地址
  VideoSourceFormat? videoSourceTabs;

  /// 作业票详情
  ReceiptInfoEntity receiptInfoEntity = ReceiptInfoEntity();

  /// 流程节点信息
  ExecuteEntity receiptInfo = ExecuteEntity();

  ///作业申请节点
  ExecuteTicketReceiptList ticketReceiptList = ExecuteTicketReceiptList();

  ///	安全措施节点
  List<ExecuteWorkSafetyMeasuresList> workSafetyMeasuresList = [];

  ///安全交底节点
  List<ExecuteTicketDiscloseList> ticketDiscloseList = [];

  ///气体分析节点
  List<ExecuteTicketGasList> ticketGasList = [];

  // 公司列表
  List<String> companyList = [
    '器具信息',
    '违规记录',
    '作业人员',
  ];

  // 公司附件列表
  List<String> companyAttachmentList = [
    '安全措施确定',
    '交底记录',
    '验收条件',
    '签字表',
    // '资质证明',
    // '中断记录',
    '气体分析记录',
  ];

  // 根据索引跳转公司列表
  void toCompanyList(int index) {
    switch (index) {
      case 0:
        toApplianceInformation();
        break;
      case 1:
        toViolationRecord();
        break;
      case 2:
        toOperatingPersonnel();
        break;
      default:
    }
  }

  // 根据索引跳转公司附件列表
  void toCompanyAttachmentList(String title) {
    switch (title) {
      case '安全措施确定':
        toSafetyMeasures();
        break;
      case '交底记录':
        toDisclosureRecord();
        break;
      case '验收条件':
        toAcceptanceConditions();
        break;
      case '签字表':
        toSignatureTable();
        break;
      case '资质证明':
        toQualificationCertificate();
        break;
      case '中断记录':
        toInterruptRecord();
        break;
      case '气体分析记录':
        toGasAnalysisRecord();
        break;
      default:
    }
  }

  // 跳转公司详情
  void toCompanyDetails() {
    Get.toNamed(AppRoutes.jobLedgerDetailCompany, arguments: receiptInfoEntity);
  }

  // 跳转器具信息
  void toApplianceInformation() {
    Get.toNamed(AppRoutes.jobLedgerDetailInstrument, arguments: receiptInfoEntity);
  }

  // 跳转违规记录
  void toViolationRecord() {
    Get.toNamed(AppRoutes.jobLedgerDetailViolationRecord, arguments: receiptInfoEntity);
  }

  // 跳转作业人员
  void toOperatingPersonnel() {
    Get.toNamed(AppRoutes.jobLedgerDetailPersonnel, arguments: receiptInfoEntity);
  }

  // 跳转安全措施确定
  void toSafetyMeasures() {
    Get.toNamed(AppRoutes.jobLedgerDetailSafetyMeasures, arguments: workSafetyMeasuresList);
  }

  // 跳转交底记录
  void toDisclosureRecord() {
    Get.toNamed(AppRoutes.jobLedgerDetailDisclosureRecord, arguments: ticketDiscloseList);
  }

  // 跳转验收条件
  void toAcceptanceConditions() {
    Get.toNamed(AppRoutes.jobLedgerDetailAcceptanceConditions, arguments: receiptInfoEntity);
  }

  // 跳转签字表
  void toSignatureTable() {
    Get.toNamed(AppRoutes.jobLedgerDetailSignatureTable, arguments: receiptInfoEntity);
  }

  // 跳转资质证明
  void toQualificationCertificate() {
    Get.toNamed(AppRoutes.jobLedgerDetailQualificationCertificate, arguments: receiptInfoEntity);
  }

  // 跳转中断记录
  void toInterruptRecord() {
    Get.toNamed(AppRoutes.jobLedgerDetailInterruptRecord, arguments: operatingLedgerReceiptLists.id);
  }

  // 跳转气体分析记录
  void toGasAnalysisRecord() {
    Get.toNamed(AppRoutes.jobLedgerDetailGasAnalysisRecord, arguments: ticketGasList);
  }

  // 跳转作业票
  void toJobTicket() {
    Get.toNamed(AppRoutes.jobTicket, arguments: {
      'id': receiptInfoEntity.id,
      'businessType': JobTicketType.getBusinessTypeByName(operatingLedgerReceiptLists.ticketBigTypeName!),
    });
  }

  // 获取作业票详情
  void getJobTicketDetails() async {
    final res = await HttpUtil().generateGetRequest<ReceiptInfoEntity>(
      Interface.getTicketReceiptDetailList,
      queryParameters: {
        'receiptId': operatingLedgerReceiptLists.id,
      },
    ).execute();

    res.onSuccess((result) {
      if (result.data != null) {
        receiptInfoEntity = result.data!;
        receiptInfo = receiptInfoEntity.receiptInfo!;
        ticketReceiptList = receiptInfo.ticketReceiptList!;
        workSafetyMeasuresList = receiptInfo.workSafetyMeasuresList!;
        ticketDiscloseList = receiptInfo.ticketDiscloseList!;
        ticketGasList = receiptInfo.ticketGasList!;
        final urls = receiptInfoEntity.receiptInfo!.ticketReceiptList!.liveVideoUrl;
        if(urls.isNotEmpty) {
          videoSourceTabs = VideoSourceFormat(
            video: [
              VideoSourceFormatVideo(
                name: '过程影像',
                list: [],
              ),
            ],
          );
          for(String i in urls) {
            videoSourceTabs!.video![0]?.list!.add(VideoSourceFormatVideoList(
              name: '视频${i.split('/').last}',
              url: i,
            ));
          }

          // playVideo(urls[0]);
        }
        update();
      }
    });
  }

  // 播放视频
  void playVideo(String url){
    player.setDataSource(url,showCover: true);
  }

  // 当前tab的index，默认0
  int curTabIdx = 0;

  // 当前选中的tablist index，默认0
  int curActiveIdx = 0;

  /// 视频配置
  ShowConfigAbs vCfg = PlayerShowConfig();

  // 播放器内部切换视频钩子，回调，tabIdx 和 activeIdx
  void onChangeVideo(int curTabIdx, int curActiveIdx) {
    // setState(() {
    //   _curTabIdx = curTabIdx;
    //   _curActiveIdx = curActiveIdx;
    // });
  }


  @override
  void onReady() {
    super.onReady();
    getJobTicketDetails();
  }

  @override
  void onClose() {
    player.release();
    super.onClose();
  }

}
